//
//  BDShopJSTagsView.m
//  iOS_beboy
//
//  Created by wen on 2017/9/23.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDShopJSTagsView.h"

@interface BDShopJSTagsView ()
{
    NSMutableArray<BDTagsModel *> *_tags;
    NSArray<BDTagsModel *> *_allTags;
}
@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, copy) void (^finishBlock)(NSArray<BDTagsModel *> *tags);

@end

@implementation BDShopJSTagsView

- (instancetype)initWithAllTags:(NSArray<BDTagsModel *> *)allTags startTags:(NSArray<BDTagsModel *> *)startTags{
    CGRect frame = [UIScreen mainScreen].bounds;
    if (self = [super initWithFrame:frame]) {
        _allTags = allTags;
        _tags = [NSMutableArray array];
        for (BDTagsModel *tag in _allTags) {
            for (BDTagsModel *startTag in startTags) {
                if (startTag.tag_id == tag.tag_id) {
                    [_tags addObject:tag];
                }
            }
        }
        _bkView = [[UIView alloc] initWithFrame:self.bounds];
        _bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        [self addSubview:_bkView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [_bkView addGestureRecognizer:tap];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            [self hidden];
        }];
        
        _contentView = [[UIView alloc] init];
        _contentView.size = CGSizeMake(295, 0);
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 4;
        _contentView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_contentView];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, _contentView.width, 0)];
        _scrollView.bounces = NO;
        [_contentView addSubview:_scrollView];
        
        CGFloat labelWidth = 65;
        CGFloat labelHeight = 26;
        CGFloat left = (_contentView.width - labelWidth * 3 - 20 * 2) / 2;
        CGFloat maxHeight = 0;
        for (int i = 0; i < _allTags.count; i++) {
            UILabel *label = [[UILabel alloc] init];
            label.tag = 5555 + i;
            label.font = [UIFont systemFontOfSize:13];
            label.textAlignment = NSTextAlignmentCenter;
            label.layer.masksToBounds = YES;
            label.layer.cornerRadius = 3;
            [_scrollView addSubview:label];
            
            BDTagsModel *curTag = _allTags[i];
            label.text = curTag.name;
            BOOL contains = NO;
            for (BDTagsModel *tag in _tags) {
                if (tag.tag_id == curTag.tag_id) {
                    contains = YES;
                    break;
                }
            }
            
            if (contains == YES) {
                label.backgroundColor = [UIColor colorWithHexString:curTag.bg_color];
                label.textColor = [UIColor colorWithHexString:curTag.text_color];
                label.accessibilityIdentifier = @"seleted";
            }else{
                label.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
                label.textColor = [UIColor colorWithHexString:@"#999999"];
                label.accessibilityIdentifier = @"noneSeleted";
            }
            
            label.size = CGSizeMake(labelWidth, labelHeight);
            label.left = left + (20 + labelWidth) * (i % 3);
            label.top = (20 + labelHeight) * (i / 3);
            maxHeight = label.bottom;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            [label addGestureRecognizer:tap];
            label.userInteractionEnabled = YES;
            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
                if ([label.accessibilityIdentifier isEqualToString:@"seleted"]) {
                    [_tags removeObject:curTag];
                    label.backgroundColor = [UIColor colorWithHexString:@"f0f0f0"];
                    label.textColor = [UIColor colorWithHexString:@"#999999"];
                    label.accessibilityIdentifier = @"noneSeleted";
                }else{
                    if (_tags.count >= 3) {
                        [MBManager showBriefAlert:ASLocalizedString(@"最多选择三个标签")clickHidden:YES clickBlock:nil];
                        return;
                    }
                    [_tags addObject:curTag];
                    label.backgroundColor = [UIColor colorWithHexString:curTag.bg_color];
                    label.textColor = [UIColor colorWithHexString:curTag.text_color];
                    label.accessibilityIdentifier = @"seleted";
                }
            }];
        }
        
        _scrollView.contentSize = CGSizeMake(0, maxHeight);
        if (maxHeight > kBDScreenHeight / 2.0) {
            _scrollView.height = kBDScreenHeight / 5.0 * 2;
        }else{
            _scrollView.height = maxHeight;
        }
        
        UILabel *tishiLabel = [[UILabel alloc] init];
        tishiLabel.font = [UIFont systemFontOfSize:11];
        tishiLabel.textColor = [UIColor colorWithHexString:@"#999999"];
        tishiLabel.text = ASLocalizedString(@"选择你要的标签");
        [tishiLabel sizeToFit];
        tishiLabel.top = _scrollView.bottom + 20;
        tishiLabel.centerX = _contentView.width / 2.0;
        [_contentView addSubview:tishiLabel];
        
        CGFloat lineWidth = (_contentView.width - tishiLabel.width - 10 * 2) / 2;
        for (int i = 0; i < 2; i++) {
            UIView *lineView = [[UIView alloc] init];
            lineView.backgroundColor = [UIColor colorWithHexString:@"#999999"];
            lineView.size = CGSizeMake(lineWidth, 0.5);
            lineView.centerY = tishiLabel.centerY;
            if (i == 0) {
                lineView.left = 0;
            }else{
                lineView.right = _contentView.width;
            }
            [_contentView addSubview:lineView];
        }
        
        UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureButton setTitle:ASLocalizedString(@"确认")forState:UIControlStateNormal];
        [sureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sureButton.backgroundColor = KBDColorBlue;
        sureButton.size = CGSizeMake(125, 30);
        sureButton.centerX = _contentView.width / 2.0;
        sureButton.top = tishiLabel.bottom + 10;
        sureButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_contentView addSubview:sureButton];
        [[sureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [self hidden];
            if (_finishBlock) {
                _finishBlock(_tags);
            }
        }];
        
        _contentView.height = sureButton.bottom + 20;
        _contentView.center = _bkView.center;
    }
    return self;
}

- (void)hidden{
    [UIView animateWithDuration:0.25 animations:^{
        self.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
        self.contentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

+ (void)showTagsViewAllTags:(NSArray<BDTagsModel *> *)allTags StartTags:(NSArray<BDTagsModel *> *)startTags Finish:(void (^)(NSArray<BDTagsModel *> *tags))finish{
    BDShopJSTagsView *tagsView = [[BDShopJSTagsView alloc] initWithAllTags:allTags startTags:startTags];
    tagsView.finishBlock = finish;
    [[UIApplication sharedApplication].keyWindow addSubview:tagsView];
    [UIView animateWithDuration:0.25 animations:^{
        tagsView.bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    }];
}

@end
